package com.sghd.fight.module.game.service.configure;

import com.sghd.fight.module.game.config.*;
import com.sghd.fight.module.game.core.model.BattleSkill;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.GameService;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.room.Monster;
import com.sghd.fight.module.game.service.room.Player;
import com.sghd.fight.module.game.service.room.PveRoom;
import com.sghd.fight.module.game.service.work.RoomTimer;
import com.sghd.pojo.fight.game.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class PveConfigure extends ConfigureTemplate {
    @Autowired
    private GameService gameService;

    @Override
    public RoomType getType() {
        return RoomType.PVE;
    }

    @Override
    public IRoom build(long roomId, RoomTimer timer,LevelVo levelVo) {
        if (getConfigService().getLevel(levelVo) == null){
            return null;
        }
        long seed = 0;
        List<Long> randoms = getConfigService().getLevel(levelVo).getRandoms();
        if (randoms.size() == 0){
            seed = (long)(Math.random() * 10000);
        }else {
            seed = randoms.get((int) (Math.random() * randoms.size()));
        }
        return new PveRoom(roomId, getType(), timer,configService, getConfigService().getLevel(levelVo),seed);
    }

    @Override
    public Monster transformByMonsterId(PlayerVo playerInfo, IRoom room, Date date) {
        LevelConfig levelConfig = configService.getLevel(playerInfo.getLevelVo());
        MonsterConfig monsterConfig = configService.getMonsterConfig(levelConfig.getMonsterId());
        Monster monster = new Monster(monsterConfig,room,levelConfig);
        Map<Integer, BattleSkill> battleSkill = monster.getSkills();
        for (Integer skillId : levelConfig.getMonsterSkills().keySet()) {
            SkillConfig skillConfig = configService.getSkillConfig(skillId);
            battleSkill.put(skillId, new BattleSkill(skillId, skillConfig.getCool(0), 0));
        }
        if (levelConfig.getSeckillId() != null){
            battleSkill.put(levelConfig.getSeckillId(),new BattleSkill(levelConfig.getSeckillId(),1, 0));
        }
        if (levelConfig.getAttackId() != null){
            battleSkill.put(levelConfig.getAttackId(),new BattleSkill(levelConfig.getAttackId(),1, 0));
        }
        monster.setMonsterConfig(monsterConfig);
        return monster;
    }

    @Override
    public Player transform(PlayerVo playerInfo, IRoom room, Date date) {
        //TODO 大厅上阵技能
        Set<Integer> resultRandom = new HashSet<>();
        Map<Integer, Integer> skill2Level = playerInfo.getSkills();


        resultRandom.addAll(playerInfo.getSkills().keySet());
        //关卡本身技能数不足5个     从玩家身上技能背包获取技能拼凑成5个
//        if (resultRandom.size() != 5){
//            if (!playerInfo.getSkills().isEmpty()){
//                //如果玩家身上技能背包 与 关卡设计技能总和大于5 则随机取  不大于5 就全部放入不足 不管
//                if (playerInfo.getSkills().size() + resultRandom.size() > 5){
//                    Integer[] integers = playerInfo.getSkills().keySet().toArray(new Integer[0]);
//                    while (resultRandom.size() < 5){
//                        Integer skillId = integers[RandomUtils.nextInt(integers.length)];
//                        resultRandom.add(skillId);
//                    }
//                    for (Integer random : resultRandom) {
//                        skill2Level.put(random,playerInfo.getSkills().get(random));
//                    }
//                }else {
//                    resultRandom.addAll(playerInfo.getSkills().keySet());
//                    for (Integer skill : playerInfo.getSkills().keySet()) {
//                        skill2Level.put(skill, playerInfo.getSkills().get(skill));
//                    }
//                }
//            }
//        }
        LevelConfig levelConfig = configService.getLevel(playerInfo.getLevelVo());
        Player player = new Player(playerInfo.getPlayerId(), playerInfo.getName(), playerInfo.getIndex(), room,
                new HashMap<>(skill2Level), new HashMap<>(playerInfo.getElements()), playerInfo.getMoney(),levelConfig);
        Map<Integer, BattleSkill> battleSkill = player.getSkills();
        for (Integer skillId : resultRandom) {
            SkillConfig skillConfig = configService.getSkillConfig(skillId);
            int level = skill2Level.get(skillId) == null?0:skill2Level.get(skillId);
            battleSkill.put(skillId, new BattleSkill(skillId, skillConfig.getCool(player.getSkillLevel(skillId)), level));
        }
        fixValue(player, room, playerInfo);
        List<Integer> list = new ArrayList<>();
        list.toArray(new Integer[0]);
        return player;
    }

    /** 修改玩家属性*/
    @Override
    public void fixValue(Unit unit, IRoom room,PlayerVo playerInfo) {
        LevelConfig levelConfig = configService.getLevel(playerInfo.getLevelVo());
        Map<UnitValue,Integer> values = unit.getValues();
        Player player = (Player) unit;
        Map<ChessType, Integer> attr2Level = player.getElement2Level();
        //五行累计提供的血量上限
        int total = 0;
        for (ChessType chessType : ChessType.values()) {
            if (chessType == ChessType.CT_LIGHTNING || attr2Level.get(chessType) == null || attr2Level.get(chessType) == 0) {
                continue;
            }
            int level = attr2Level.get(chessType);
            for (; level > 0; level--) {
                ElementPk pk = ElementPk.valueOf(chessType, level);
                ElementConfig attrConfig = configService.getAttr(pk);
                total += attrConfig.getAttr(room.getType()).getUpHp();
            }
        }
        values.put(UnitValue.HP,levelConfig.getPlayerHp() + total);
        values.put(UnitValue.HP_MAX,levelConfig.getPlayerHpMax() + total);
    }
}
